package com.mjk.common.core.jdbc.util;


import com.mjk.common.base.model.BaseEntity;
import com.mjk.common.base.model.BaseExample;
import com.mjk.common.core.jdbc.annotation.ArcSharding;
import com.mjk.common.core.jdbc.datasource.CommonDataSourceAutoConf;
import com.mjk.common.core.jdbc.datasource.DynamicDataSource;
import com.mjk.common.core.jdbc.datasource.ShardingDataSouceAutoConf;
import com.mjk.common.core.jdbc.service.plus.constns.PlusConstns;
import com.mjk.common.core.jdbc.service.plus.provider.BeeDataSourceType;
import com.mjk.common.core.jdbc.service.plus.provider.interfaces.IBeeProvider;
import com.mjk.common.core.jdbc.util.provider.SqlProvider;
import com.mjk.common.tools.springextend.BeeSpringBean;
import com.mjk.common.util.ObjectUtils;

/**
 * 表相关工具类
 *
 * @author mjk
 */
public class TableUtils {
    /**
     * 获取 解析 实体类
     */
    public static IBeeProvider getHandler(BaseEntity t) {
        String driveName = getDriverName(t);
        return BeeDataSourceType.getHandler(driveName, PlusConstns.EXAMPLE);
    }

    public static IBeeProvider getHandler(BaseExample t) {
        BaseEntity entity = SqlProvider.getEntityFromExample(t);
        return getHandler(entity);
    }

    /**
     * 获取表明
     */
    public static String getTableName(BaseEntity entity) {
        ArcSharding arc = ShardingUtils.getArcSharding(entity);
        if (null != arc) {
            try {
                return getHandler(entity).getTabName(arc);
            } catch (Exception e) {
                return arc.table();
            }
        } else {
            return "";
        }
    }


    /**
     * 获取数据源
     */
    public static String getDataSource(BaseEntity entity) {
        ArcSharding arc = ShardingUtils.getArcSharding(entity);
        if (null != arc) {
            return arc.dataSource();
        } else {
            return "";
        }
    }

    /**
     * 获取驱动名称
     */
    public static String getDriverName() {
        BaseEntity t = null;
        return getDriverName(t);
    }

    public static String getDriverName(BaseExample t) {
        BaseEntity entity = SqlProvider.getEntityFromExample(t);
        return getDriverName(entity);
    }

    public static String getDriverName(BaseEntity t) {
        return getDriverName(t, "");
    }

    @SuppressWarnings("static-access")
    public static String getDriverName(BaseEntity t, String defaultType) {
        if (t != null) {
            ArcSharding sharding = ShardingUtils.getArcSharding(t);
            if (null != sharding && ObjectUtils.isNotEmpty(sharding.dataSource())) {
                DynamicDataSource.setDatabaseType(sharding.dataSource());
            }
        }

        DynamicDataSource dataSource = BeeSpringBean.getBean(DynamicDataSource.class);
        CommonDataSourceAutoConf conf = BeeSpringBean.getBean(CommonDataSourceAutoConf.class);
        if (null == conf) {
            ShardingDataSouceAutoConf sConf = BeeSpringBean.getBean(ShardingDataSouceAutoConf.class);
            if (null == sConf) {
                return defaultType;
            }
            String result = sConf.getDriverMap().get(ObjectUtils.isEmpty(dataSource.getDatabaseType()) ? defaultType : dataSource.getDatabaseType());
            if (null == result) {
                return sConf.getDefaultName();
            }
            return result;
        } else {
            String result = conf.getDriverMap().get(ObjectUtils.isEmpty(dataSource.getDatabaseType()) ? defaultType : dataSource.getDatabaseType());
            if (null == result) {
                return conf.getDefaultName();
            }
            return result;
        }
    }

}
